#include "echo.h"
#include "log.h"
#include <functional>
void EchoServer::connectHandler(const ConnectionPtr &conn) 
{
    LOG(Debug, "NEW CONNECTION:%p", conn.get());
}
void EchoServer::closedHandler(const ConnectionPtr &conn) {
    LOG(Debug, "CLOSE CONNECTION:%p", conn.get());
}
void EchoServer::MessageHandler(const ConnectionPtr &conn, Buffer *buf) 
{
    conn->send(buf->getReadAddr(), buf->getReadSize());
    buf->moveReadStep(buf->getReadSize());
    conn->shutDown();
}

EchoServer::EchoServer(int port, const std::string& ip)
    :m_server(port, ip) 
{
    m_server.setThreadCount(2);
    m_server.enableInactiveRelease(10);
    m_server.setCloseCallback(std::bind(&EchoServer::closedHandler, this, std::placeholders::_1));
    m_server.setConnectCallback(std::bind(&EchoServer::connectHandler, this, std::placeholders::_1));
    m_server.setMessageCallback(std::bind(&EchoServer::MessageHandler, this, std::placeholders::_1, std::placeholders::_2));
}

void EchoServer::start() 
{ 
    m_server.start(); 
}